﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainFinanceAccountsCode : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public FinAccountsEntity SelectedAccount { get; set; }
        #endregion

        #region Constructor
        public frmMaintainFinanceAccountsCode()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            DataTable dt = FinAccountsBs.GetAccountsType();
            if (dt != null)
            {
                cmbAccountType.DataSource = dt;
                cmbAccountType.DisplayMember = "ACCOUNTS_TYPE";
                cmbAccountType.ValueMember = "ACCOUNTS_TYPE";
            }
        }
        private void SetFields()
        {
            if (SelectedAccount == null)
            {
                return;
            }
            txtAccountCode.Text = SelectedAccount.AccountsCode;
            txtAccountName.Text = SelectedAccount.AccountsName;
            if (string.IsNullOrEmpty(SelectedAccount.ParentCode) == false)
            {
                FinAccountsEntity parent = FinAccountsBs.GetByCode(SelectedAccount.ParentCode);
                txtParentAccount.Tag = parent;
                txtParentAccount.Text = parent.AccountsName;
            }
            else
            {
                txtParentAccount.Tag = null;
                txtParentAccount.Text = "";
            }
            cmbAccountType.SetSelectedValue(SelectedAccount.AccountsType);
        }
        private void SetEntity()
        {
            if (SelectedAccount == null)
            {
                SelectedAccount = new FinAccountsEntity();
            }
            SelectedAccount.AccountsCode = txtAccountCode.Text;
            SelectedAccount.AccountsName = txtAccountName.Text;
            SelectedAccount.AccountsType = cmbAccountType.Text;
            if (txtParentAccount.Tag != null)
            {
                SelectedAccount.ParentCode = ((FinAccountsEntity)txtParentAccount.Tag).AccountsCode;
            }
        }
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtAccountCode.Text))
            {
                MessageBox.Show("科目编号不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtAccountName.Text))
            {
                MessageBox.Show("科目名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = FinAccountsBs.InsertFinAccounts(SelectedAccount);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = FinAccountsBs.UpdateFinAccounts(SelectedAccount);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtParentAccount_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainFinanceAccountsCodeQuery frm = new frmMaintainFinanceAccountsCodeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAccount != null)
            {
                txtParentAccount.Text = frm.SelectedAccount.AccountsName;
                txtParentAccount.Tag = frm.SelectedAccount;
            }
            else
            {
                txtParentAccount.Text = "";
                txtParentAccount.Tag = null;
            }
        }
        #endregion


    }
}
